﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projecturo.Model;
using Neits.UserService;
using System.Threading;
using Projecturo.ProjecturoClient.Connection;

namespace Projecturo.ProjecturoClient.ViewModel.Project
{
    public class NoteViewModel : WorkspaceViewModel
    {
        private Note note;

        public NoteViewModel()
        {
            note = new Note();
            note.CreatedBy = Thread.CurrentPrincipal as IUser;
            note.CreateDate = DateTime.Now;
        }

        public override string DisplayName
        {
            get
            {
                return String.Format("Note for [{0}]", Ticket.Name);
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        private Note Note
        {
            get { return this.note; }
            set { this.note = value; }
        }

        public Int64 Id
        {
            get { return Note.Id; }
        }

        public String Message
        {
            get { return Note.Message; }
            set
            {
                if (Note.Message == value)
                    return;
                Note.Message = value;
                OnPropertyChanged("Message");
            }
        }

        public Ticket Ticket
        {
            get { return Note.Ticket; }
            set
            {
                if (Note.Ticket == value)
                    return;
                Note.Ticket = value;
                OnPropertyChanged("Ticket");
            }
        }

        public IUser CreatedBy
        {
            get { return Note.CreatedBy; }
        }

        public DateTime CreateDate
        {
            get { return Note.CreateDate; }
        }

        public DateTime ChangeDate
        {
            get { return Note.ChangeDate; }
        }

        protected override void Save()
        {
            base.Save();
            Connector.Connectivity.Storage.Save(Note);

            ViewModelBase parent = ViewModelManager.Instance.GetViewModel(this.ViewModelReference.ParentViewModelId);
            parent.Update();
        }

        public override void SetModel(object model)
        {
            Note = model as Note;
        }

        public override void SetParent(object parent)
        {
            Ticket = parent as Ticket;
        }
    }
}
